package com.resume.controller;

import com.resume.bean.DataTableResponse;
import com.resume.bean.Result;
import com.resume.bean.ResultUtil;
import com.resume.constant.GlobalConst;
import com.resume.domain.XueLi;
import com.resume.enums.ResultEnum;
import com.resume.ex.ResumeException;
import com.resume.service.XueLiService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 梁云亮
 * @Date: 2021/7/13 17:25
 * @Describe:
 */
@Slf4j
@Controller
@RequestMapping("/xueLi")
public class XueLiController {

    @Resource
    private XueLiService xueLiService;

    @ApiOperation(value = "打开简历列表页面", notes = "打开简历列表页面")
    @GetMapping("/openXueLi")
    public String openXueLi() {
        return "base/xueLi";
    }

    @ResponseBody
    @GetMapping("/v1/list")
    public Result<DataTableResponse<XueLi>> list() {
        List<XueLi> xueLiList = xueLiService.listAllUsableXueLi();

        DataTableResponse<XueLi> dataTableResponse = new DataTableResponse<>();
        dataTableResponse.setSEcho(3);
        dataTableResponse.setITotalRecords(80);
        dataTableResponse.setITotalDisplayRecords(70);
        dataTableResponse.setAaData(xueLiList);
        return ResultUtil.success(200, "数据查询成功", dataTableResponse);
    }

    @ApiOperation(value = "删除简历", notes = "根据编号删除简历")
    @ResponseBody
    @GetMapping("/v1/remove")
    public Result remove(Integer id) {
        try {
            xueLiService.modifyXueLiStatus(id, GlobalConst.Common.STATUS_DISABLE);
            return ResultUtil.success(ResultEnum.SUCCESS);
        } catch (Exception e) {
            throw new ResumeException(ResultEnum.SQL_ERROR);
        }
    }

    @ApiOperation(value = "修改简历", notes = "根据编号修改简历")
    @ResponseBody
    @GetMapping("/v1/modify")
    public Result modify(XueLi xueLi) {
        try {
             xueLiService.modify(xueLi);
            return ResultUtil.success(ResultEnum.SUCCESS);
        } catch (Exception e) {
            throw new ResumeException(ResultEnum.SQL_ERROR);
        }
    }

    @ApiOperation(value = "增加简历", notes = "增加简历")
    @ResponseBody
    @GetMapping("/v1/create")
    public Result create(XueLi xueLi) {
        try {
            xueLiService.save(xueLi);
            return ResultUtil.success(ResultEnum.SUCCESS);
        } catch (Exception e) {
            throw new ResumeException(ResultEnum.SQL_ERROR);
        }
    }

}
